<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="70%"
    :before-close="handleClose"
  >
    <el-form
      ref="formData"
      :model="formData"
      label-width="100px"
      label-postion="right"
      status-icon
    >
      <el-form-item label="标题:">
        <el-input v-model="formData.title" readonly></el-input>
      </el-form-item>

      <el-form-item label="标签:">
        <el-cascader
          style="display: block"
          disabled
          :options="labelOptions"
          v-model="formData.labelIds"
          :props="{
            multiple: true,
            children: 'labelList',
            value: 'id',
            label: 'name',
            emitPath: false,
          }"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="主图:">
        <img
          :src="formData.imageUrl"
          alt=""
          style="width: 178px; height: 178px; display: block"
        />
      </el-form-item>
      <el-form-item label="是否公开:">
        <el-radio-group v-model="formData.ispublic" disabled>
          <el-radio :label="0">公开</el-radio>
          <el-radio :label="1">不公开</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注:">
        <el-input
          type="textarea"
          v-model="formData.summary"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item label="内容:">
        <mavonEditor v-model="formData.mdContent" :editable="false" />
      </el-form-item>
      <el-form-item align="center" v-if="isAudit" size="mini">
        <el-button type="primary" @click="auditSuccess()">审核通过</el-button>
        <el-button type="danger" @click="auditFail()">审核不通过</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import api from "@/api/article";
//引入markdown的组件和样式
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";

export default {
  props: {
    id: null,
    isAudit: true, //是否为审核页面
    title: {
      //弹窗的标题
      type: String,
      default: "",
    },
    visible: {
      //弹出窗口
      type: Boolean,
      default: false,
    },

    remoteClose: Function, //用于关闭窗口
  },
  data() {
    return {
      formData: {},
      labelOptions: [],
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.getLabelOptions();
        //val为true则开始请求
        this.getArticleById();
      }
    },
  },
  components: { mavonEditor },

  methods: {
    handleClose() {
      this.remoteClose();
    },
    //审核通过
    auditSuccess() {
      this.$confirm("确认审核通过吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.auditSuccess(this.id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
            this.remoteClose();
          });
        })
        .catch(() => {});
    },
    //审核不通过
    auditFail() {
      this.$confirm("确认审核不通过吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.auditFail(this.id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
            this.remoteClose();
          });
        })
        .catch(() => {});
    },
    //查询文章详情
    async getArticleById() {
      api.getById(this.id);
      const { data } = await api.getById(this.id);
      this.formData = data;
    },
    async getLabelOptions() {
      const { data } = await api.getCategoryAndLabel();
      this.labelOptions = data;
    },
  },
};
</script>

<style scoped>
</style>